package sk.fei.vrml.objects;

import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL;
import sk.fei.vrml.main.Constants;

import com.sun.opengl.util.texture.TextureData;

public class ImageTextureObject extends TextureObject{
	
	public URL textureUrl;
	public boolean repeatS = true;
	public boolean repeatT = true;
	private boolean converted = false;
	
	// data pre texturu
	public TextureData textureData ;
	private int[] textureId;
	
	public ImageTextureObject() {
	}
	
	public ImageTextureObject(URL textureUrl,boolean repeatS,boolean repeatT,TextureData textureData){
		this.textureUrl = textureUrl;
		this.repeatS = repeatS;
		this.repeatT = repeatT;
		this.textureType = Constants.ImageTexture;
		this.textureData = textureData;
		this.textureId = new int[1];
	}
	
	public void convertTextureDataToTexture(GL gl){
		if (!converted){
		ByteBuffer directBuf = ByteBuffer.allocateDirect(textureData.getBuffer().capacity());
		directBuf.put((ByteBuffer)textureData.getBuffer());
		directBuf.flip();
		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glGenTextures(1, IntBuffer.wrap(textureId));
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureId[0]);	
		//System.out.println( textureData.getPixelFormat()+ " "+ GL.GL_RGBA +" "+textureData.getPixelType());	
		gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 4, textureData.getWidth(), textureData.getHeight(), textureData.getBorder(), textureData.getPixelFormat(), textureData.getPixelType(), directBuf);	
		converted = true;
		gl.glDisable(GL.GL_TEXTURE_2D);
		}
	}
	
	public void bindTexture(GL gl){
		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glBindTexture(GL.GL_TEXTURE_2D, textureId[0]);
	}

	
	public int getTextureId(){
		return textureId[0];
	}

	public boolean isRepeatS() {
		return repeatS;
	}

	public void setRepeatS(boolean repeatS) {
		this.repeatS = repeatS;
	}

	public boolean isRepeatT() {
		return repeatT;
	}

	public void setRepeatT(boolean repeatT) {
		this.repeatT = repeatT;
	}

	public URL getTextureUrl() {
		return textureUrl;
	}

	public void setTextureUrl(URL textureUrl) {
		this.textureUrl = textureUrl;
	}
	
	
}